
use Getopt::Long;
#reading input arguments
&Getopt::Long::GetOptions(
'REF_FILE=s' => \$reffile,
'INPUT_FILE=s' => \$inputfile,
'OUTPUT_FILE=s' => \$outputfile
);
chomp($inputfile);
chomp($reffile);
chomp($outputfile);
#checking for missing arguments
if($reffile eq "" || $inputfile eq "" ||  $outputfile eq "" )
{
	die "missing arguments\n USAGE : perl perl_create_reffile.pl -INPUT_FILE <INPUT FILE> -REF_FILE <REF FILE> -OUTPUT_FILE <OUTPUT FILE>\n";
}
open(BUFF,"$reffile") or die " no file found $reffile\n";
$num=0;
my @ref;
my @id;
my %ref;
#reading the reffile and creating the hash
while(<BUFF>)
{
	chomp($_);
	$ref{$_}=$num;
	$ref[$num]="NA";
	$id[$num]=$_;
	$num++;
}

#reading the input file
open(BUFF,"$inputfile") or die " no file found $inputfile\n";

$line=<BUFF>;
while(<BUFF>)
{
	chomp($_);
	@array=split("\t",$_);
	$array[1] =~ s/^.*\|//g;
	#print "$array[1]\n";
	if(exists($ref{$array[1]}))
	{
		$ref[$ref{$array[1]}]=$array[2];
	}
	else
	{
		die "value $array[1] not found in the reference $ref\n";
	}
}	
close(BUFF);

#writing the output file
open(WRBUFF,">$outputfile") or die "not able to write $outputfile\n";
print WRBUFF "SAMPLE\t$array[0]\n";
for($i=0;$i<@id;$i++)
{
	print WRBUFF "$id[$i]\t$ref[$i]\n";
}
close(WRBUFF);